package com.neuedu.service.impl;

import com.neuedu.bean.*;
import com.neuedu.mapper.CartMapper;
import com.neuedu.mapper.OrdersMapper;
import com.neuedu.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Autowired
    private CartService cartService;

    @Autowired
    private OrderDetailetService orderDetailetService;

    @Autowired
    private BusinessService businessService;

    @Autowired
    private FoodService foodService;



    @Override
    public int createOrders(Orders orders) throws Exception {
        // 订单的日期
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String orderDateStr = simpleDateFormat.format(new Date());
        orders.setOrderDate(orderDateStr);
        // 订单的状态，默认为0，表示未支付
        orders.setOrderState(0);
        // 保存订单的基本信息到数据库
        ordersMapper.createOrders(orders);

        // 保存订单的详情
        Cart cart = new Cart();
        cart.setBusinessId(orders.getBusinessId());
        cart.setUserId(orders.getUserId());
        List<Cart> cartList = cartService.listCart(cart);

        List<OrderDetailet> list = new ArrayList<>();
        for (Cart c : cartList) {
            OrderDetailet orderDetailet = new OrderDetailet();
            orderDetailet.setOrderId(orders.getOrderId());
            orderDetailet.setFoodId(c.getFoodId());
            orderDetailet.setQuantity(c.getQuantity());
            list.add(orderDetailet);

        }
        // 批量保存订单的详情
        orderDetailetService.saveOrderDetailetBatch(list);

        // TODO: 订单详情保存完毕之后，清空购物车

        return orders.getOrderId();
    }

    @Override
    public Orders getOrdersById(Integer orderId) throws Exception {
        // 查询该订单的基本信息
        Orders orders = ordersMapper.getOrdersById(orderId);

        if (orders != null) {

            // 封装商家信息
            Business business = businessService.getBusinessById(orders.getBusinessId()+"");
            orders.setBusiness(business);

            // 封装订单对应的菜品信息
            List<OrderDetailet> orderDetailets = orderDetailetService.listOrderDetailetByOrderId(orderId);
            for (OrderDetailet orderDetailet : orderDetailets) {
                Integer foodId = orderDetailet.getFoodId();
                Food food = foodService.getFoodById(foodId);
                orderDetailet.setFood(food);
            }

            // 再把集合添加到订单对象中
            orders.setList(orderDetailets);

        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) throws Exception {
        List<Orders> ordersList = ordersMapper.listOrdersByUserId(userId);
        // 遍历订单列表，进行封装数据
        for (Orders orders : ordersList) {
            // 封装商家信息
            Business business = businessService.getBusinessById(orders.getBusinessId()+"");
            orders.setBusiness(business);

            List<OrderDetailet> orderDetailets = orderDetailetService.listOrderDetailetByOrderId(orders.getOrderId());
            for (OrderDetailet orderDetailet : orderDetailets) {
                Integer foodId = orderDetailet.getFoodId();
                Food food = foodService.getFoodById(foodId);
                orderDetailet.setFood(food);
            }

            orders.setList(orderDetailets);
        }
        return ordersList;
    }

    @Override
    public int updateOrders(Integer orderId) throws Exception {
        // TODO: 订单的状态更新
        int i = ordersMapper.updateOrders(orderId);
        return i;
    }

}
